//
//  CategoryDetailViewController.m
//  悦读
//
//  Created by dllo on 15/5/23.
//  Copyright (c) 2015年 贾建兴. All rights reserved.
//

#import "CategoryDetailViewController.h"
#import "SANetWorkingTask.h"
#import "MyFMCategotyDetailCell.h"
#import "BookDetailController.h"
#import "CollectClass.h"
@interface CategoryDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

// 定义table便于使用
@property(nonatomic, retain)UITableView *table;
// 定义tableArray装请求来的tableview中的所有数据
@property(nonatomic, retain)NSMutableArray *tableArray;

@end

@implementation CategoryDetailViewController
- (void)dealloc
{
    [_categoryName release];
    [_cid release];
    [_pxId release];
    [_tableArray release];
    [_table release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // 初始化数组
        self.tableArray = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createTable];
    
    // 设置标题
    self.navigationItem.title = self.categoryName;
    
    if (self.choice == 1) {
        // 判断是否男生女生
        
        
        [self dataRequest];
        
    } else
    {
        [self dataRequestGender];
    }
    
}

#pragma mark -
#pragma mark 建table
- (void)createTable
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64) style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    // 不弹
    self.table.bounces = NO;
    [self.view addSubview:self.table];
    
}

#pragma mark -
#pragma mark 申请数据
- (void)dataRequest
{
    NSString *request = @"http://vip.hentr.com/zhuibook/api/zhuibook/categoryList.do";
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"1" forKey:@"version"];
    [dic setObject:@"1" forKey:@"page"];
    [dic setObject:self.cid forKey:@"cId"];
    [SANetWorkingTask request:request parmaters:dic block:^(NSDictionary *result) {
       
        self.tableArray = [[result objectForKey:@"response"] objectForKey:@"ReturnData"];
//        NSLog(@"self.tableArray = %@", self.tableArray);
        [self.table reloadData];
        
        
    }];
}

- (void)dataRequestGender
{
    NSString *request = @"http://vip.hentr.com/zhuibook/api/zhuibook/allBookList.do";
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"1" forKey:@"version"];
    [dic setObject:@"1" forKey:@"page"];
    [dic setObject:self.pxId forKey:@"pxId"];
    [SANetWorkingTask request:request parmaters:dic block:^(NSDictionary *result) {
        
        self.tableArray = [[result objectForKey:@"response"] objectForKey:@"ReturnData"];
//        NSLog(@"self.tableArray = %@", self.tableArray);
        [self.table reloadData];
        
        
    }];
}

#pragma mark - table的协议实现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIn = @"cell";
    MyFMCategotyDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIn];
    if (!cell) {
        cell = [[MyFMCategotyDetailCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIn];
    }
    
    // 设置点击不变色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    // 取出行所对应的数字元素
    NSMutableDictionary *dic = [self.tableArray objectAtIndex:indexPath.row];
    // 给cell赋值
    [cell addDataToView:dic];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}
#pragma mark -
#pragma mark - 点击cell跳转页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
       //跳到书籍详情页面 传id过去
    
    // 点击某行跳到相对应的section所在的视图
    // 需要传值:storyType对应的value传给下一个页面的pxId
    NSMutableDictionary *dic =  [self.tableArray objectAtIndex:indexPath.row];
    
    BookDetailController *detail = [[BookDetailController alloc]init];
    // 传值
   
//    detail.delegate = self;
    
    detail.str = [NSString stringWithFormat:@"%@",
                  [dic objectForKey:@"bookid"]];
    
    // 影藏tabbar, 回来时就又有了
    detail.hidesBottomBarWhenPushed =YES;
    // 跳转页面
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];

}

//#pragma mark - 书籍详情cell里的协议
//- (void)AddShelf:(NSString *)bookId
//{
//    // 保存小说的内容到本地
//    // 从路径中取数组, 保存书架里的小说内容
//
//    // 从tableArray中取出具体字典
//    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
//    for (NSMutableDictionary *dic0 in self.tableArray)
//    {
//
//        if ([bookId isEqualToString:[NSString stringWithFormat:@"%@",[dic0 objectForKey:@"bookid"]]])
//        {
//            dic = dic0;
//        }
//
//    }
//
//    [CollectClass writeArray:dic];
//
//
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
